home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 May: Tool Chest / Dev.CD May 97 TC.toast / Sample Code / Networking / Http Server / •Mac_Classes / TMacException.h < prev    next >
Encoding:
Text File  |  1996-01-11  |  3.1 KB  |  98 lines  |  [TEXT/CWIE]

  1. //    TMacException.h - Exception class object
  2. // 
  3. // Apple Macintosh Developer Technical Support
  4. // Written by:  Vinne Moscaritolo
  5. //
  6. //  Copyright (work in progress)  Apple Computer, Inc All rights reserved.
  7. //
  8. // You may incorporate this sample code into your applications without
  9. // restriction, though the sample code has been provided "AS IS" and the
  10. // responsibility for its operation is 100% yours.  However, what you are
  11. // not permitted to do is to redistribute the source as "DSC Sample Code"
  12. // after having made changes. If you're going to re-distribute the source,
  13. // we require that you make it clear in the source that the code was
  14. // descended from Apple Sample Code, but that you've made changes.
  15. // 
  16.  
  17. #ifndef _H_TMACEXCEPTION
  18. #define _H_TMACEXCEPTION
  19.  
  20. // ______________________________________________________________________________
  21. // MACROS
  22.  
  23. // This macro will automatically fill in the __FILE__ and __LINE statements
  24. // when a throw is used.
  25.  
  26. #define THROWEXCEPTION(name, number) \
  27.     throw TMacException( (name), (number), __FILE__, __LINE__)
  28.  
  29.  
  30. // ______________________________________________________________________________
  31. // EXCEPTION CLASSES
  32.  
  33. class TMacException 
  34. {
  35. public:
  36.     TMacException(const OSErr theError) :
  37.                     fMessage("NO MESSAGE SPECIFIED"), 
  38.                     fError(theError), 
  39.                     fFileName("NO FILE SPECIFIED"), 
  40.                     fLineNumber(0L) 
  41.                     {};
  42.  
  43.     TMacException(const char *theMessage, const OSErr theError) :
  44.                     fMessage(theMessage), 
  45.                     fError(theError), 
  46.                     fFileName("NO FILE SPECIFIED"), 
  47.                     fLineNumber(0L) 
  48.                     {};
  49.                     
  50.     TMacException(const char *theMessage, const OSErr theError, const char *theFileName, const long theLineNumber) :
  51.                     fMessage(theMessage), 
  52.                     fError(theError), 
  53.                     fFileName(theFileName), 
  54.                     fLineNumber(theLineNumber) 
  55.                     {};
  56.                     
  57.     const char*     GetExceptionMessage(void)     { return fMessage;};
  58.     const OSErr        GetExceptionOSErr(void)        { return fError;};
  59.     const char*        GetExceptionFile(void)        { return fFileName;};
  60.     const long        GetExceptionLine(void)        { return fLineNumber;};
  61.     
  62. protected:
  63.     const OSErr     fError;
  64.     const char*     fMessage;
  65.     const char*     fFileName;
  66.     const long        fLineNumber;
  67. };
  68.  
  69.  
  70. // Utility Macros
  71.  
  72. #define ThrowIfNil( _val_ )                                        \
  73.         if ( (_val_) == nil )                                        \
  74.             throw TMacException( "UNEXPECTED NIL RETURNED" ,0 , __FILE__, __LINE__)     
  75.  
  76. #define ThrowIfNotNil( _val_ )                                                \
  77.         if ( (_val_) != nil )                                                    \
  78.             throw TMacException( "EXPECTED NIL" ,0 , __FILE__, __LINE__) 
  79.  
  80. #define ThrowIfOSErr(_err_)                                        \
  81.         if ((_err_) != noErr)                                            \
  82.             throw TMacException( "NO MESSAGE SPECIFIED", (_err_), __FILE__, __LINE__)     
  83.  
  84. #define ThrowOSErr(_err_)                                                \
  85.             throw TMacException( "NO MESSAGE SPECIFIED", (_err_), __FILE__, __LINE__)     
  86.  
  87. #define ThrowMsgIfOSErr(_err_, _Msg_)                                        \
  88.         if ((_err_) != noErr)                                            \
  89.             throw TMacException( (_Msg_), (_err_), __FILE__, __LINE__)     
  90.  
  91. #define ThrowMsgOSErr(_err_, _Msg_)                                        \
  92.             throw TMacException( (_Msg_), (_err_), __FILE__, __LINE__)     
  93.  
  94. #define ThrowMsg( _Msg_)                                        \
  95.             throw TMacException( (_Msg_), noErr, __FILE__, __LINE__)     
  96.  
  97. #endif
  98.